package com.veeder.datacollection.alarm;

import java.util.Date;

import org.apache.commons.csv.CSVRecord;

import com.veeder.datacollection.fileHandler.CSVStorable;
import com.veeder.datacollection.util.DateUtil;

public class Alarm implements CSVStorable{

	public static String[] File_HEADERS = {"DeviceID","AlarmCategory","AlarmCode","AlarmState","Description","ReportTime","Posted"};

	public enum AlarmState {
		Occured, Ack, Cleared;
	}
	private AlarmId alarmId;

	private AlarmState state;
	
	private String description;
	
	private Date reportTime;
	
	private boolean posted=false;
	
	public Alarm(){
		super();
	}

	public Alarm(AlarmId alarmId, String description, AlarmState state, Date reportTime, boolean posted) {
		this.alarmId = alarmId;
		this.description = description;
		this.state = state;
		this.reportTime = reportTime;
		this.posted=posted;
	}
	
	public Alarm(CSVRecord record) throws Exception {
		this.alarmId = new AlarmId(Integer.parseInt(record.get(File_HEADERS[0])), record.get(File_HEADERS[1]), record.get(File_HEADERS[2]));
		this.state = AlarmState.values()[Integer.parseInt(record.get(File_HEADERS[3]))];
		this.description = record.get(File_HEADERS[4]);
		this.reportTime = DateUtil.convertToDate(record.get(File_HEADERS[5]), DateUtil.DateFormat3);
		this.posted=Boolean.parseBoolean(record.get(File_HEADERS[6]));
	}
	
	public AlarmId getAlarmId() {
		return alarmId;
	}

	public void setAlarmId(AlarmId alarmId) {
		this.alarmId = alarmId;
	}

	public Date getReportTime() {
		return reportTime;
	}

	public void setReportTime(Date reportTime) {
		this.reportTime = reportTime;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public AlarmState getState() {
		return state;
	}

	public void setState(AlarmState state) {
		this.state = state;
	}
	
	public boolean isPosted() {
		return posted;
	}

	public void setPosted(boolean posted) {
		this.posted = posted;
	}

	@Override
	public String[] toStringArray() {
		return toString().split(",");
	}

	@Override
	public String toString() {
		return String.format("%02d,%s,%s,%s,%s,%s,%s", alarmId.getDeviceId(), alarmId.getCategory(), alarmId.getCode(), state.ordinal(),description==null?"":description, DateUtil.formatDate(reportTime, DateUtil.DateFormat3),posted);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((alarmId == null) ? 0 : alarmId.hashCode());
		result = prime * result + ((reportTime == null) ? 0 : reportTime.hashCode());
		result = prime * result + ((state == null) ? 0 : state.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Alarm other = (Alarm) obj;
		if (alarmId == null) {
			if (other.alarmId != null)
				return false;
		} else if (!alarmId.equals(other.alarmId))
			return false;
		if (reportTime == null) {
			if (other.reportTime != null)
				return false;
		} else if (!reportTime.equals(other.reportTime))
			return false;
		if (state != other.state)
			return false;
		return true;
	}

	
}
